package com.xhj.xmly.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.xhj.xmly.R;
import com.xhj.xmly.adapter.HosterAdapter;
import com.xhj.xmly.bean.HosterEntity;
import com.xhj.xmly.callback.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class Discover_hoster_frag extends Fragment implements Callback<HosterEntity> {


    private RecyclerView recyclerView;
    List<HosterEntity.ListBeanX> list;
    private HosterAdapter adapter;

    public Discover_hoster_frag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover_hoster_frag, container, false);
        recyclerView = ((RecyclerView) view.findViewById(R.id.discover_hoster_recycler));
        initData();
        return view;
    }

    private void initData() {
        //下载数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService service = retrofit.create(HttpService.class);
        service.gethoster().enqueue(this);
        list  = new ArrayList<>();
        adapter = new HosterAdapter(getContext(),list);
        recyclerView.setAdapter(adapter);

    }


    @Override
    public void onResponse(Call<HosterEntity> call, Response<HosterEntity> response) {
        HosterEntity body = response.body();
        List<HosterEntity.ListBeanX> hosterlist = body.getList();
        list.addAll(hosterlist);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onFailure(Call<HosterEntity> call, Throwable t) {

        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
